import json
import os
import sys
import psycopg2
from QRoute import QRoute
from collections import namedtuple
from PyQt5 import QtWidgets
from Page import Login

dirname = os.path.dirname('E:/Users/刘海明/AppData/Local/Programs/Python/Python38/')
plugin_path = os.path.join(dirname, 'Lib', 'site-packages', 'PyQt5', 'Qt5', 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path

class Units(object):
    def dbConnect(self, pgsql):
        return psycopg2.connect(
            database = pgsql.name,
            user = pgsql.user,
            password = pgsql.password,
            host = pgsql.ip,
            port = pgsql.port
        )

    def dbDisconnect(self, conn):
        conn.commit()
        conn.close()

    def dict2obj(self, object):
        if isinstance(object, dict):
            return namedtuple(
                'struct', object.keys()
            )(
                *(self.dict2obj(val) for val in object.values())
            )
        elif isinstance(object, (tuple, list, set, frozenset)):
            return type(object)(self.dict2obj(_) for _ in object)
        else:
            return object

    def getKeys(self, data):
        keysAll_list = []
        def getkeys(data):  # 遍历json所有key
            if (type(data) == type({})):
                keys = data.keys()
                for key in keys:
                    value = data.get(key)
                    if (type(value) != type({}) and type(value) != type([])):
                        keysAll_list.append(key)
                    elif (type(value) == type({})):
                        keysAll_list.append(key)
                        getkeys(value)
                    elif (type(value) == type([])):
                        keysAll_list.append(key)
                        for para in value:
                            if (type(para) == type({}) or type(para) == type([])):
                                getkeys(para)
                            else:
                                keysAll_list.append(para)
        getkeys(data)
        return keysAll_list

    # 检测目标字段tagkey是否在data(json数据)中
    def isExtend(self, data, tagkey):
        if (type(data) != type({})):
            print('please input a json!')
        else:
            key_list = self.getKeys(data)
            for key in key_list:
                if (key == tagkey):
                    return True
        return False

    def readJsonFile(self, jsonFile):
        data = False
        if os.path.isfile(jsonFile):
            with open(jsonFile, 'r', encoding='utf-8') as f:
                if os.path.getsize(jsonFile) != 0:
                    data = self.dict2obj(json.load(f))
        return data

    def writeJsonFile(self, jsonFile, jsonData):
        with open(jsonFile, 'w', encoding='utf-8') as f:
            json.dump(jsonData, f, ensure_ascii=False)
        return jsonData

if __name__ == '__main__':
    unit = Units()
    application = QtWidgets.QApplication(sys.argv)
    window = QRoute(Login.Widget, (unit.readJsonFile("./stockSetting.json"), unit))
    sys.exit(application.exec_())